package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 大河网
 * @author Administrator
 * abc
 */
public class DaheNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://news.dahe.cn/[0-9]{4}/[0-9]{2}[-][0-9]{2}/[0-9]{9}.html",
				"http://opinion.dahe.cn/[0-9]{4}/[0-9]{2}[-][0-9]{2}/[0-9]{9}.html",
				"http://ent.dahe.cn/[0-9]{4}/[0-9]{2}[-][0-9]{2}/[0-9]{9}.html",
				"http://law.dahe.cn/[0-9]{4}/[0-9]{4}/[0-9]{5}.html",
				"http://photo.dahe.cn/[0-9]{4}/[0-9]{2}[-][0-9]{2}/[0-9]*.html",
				"http://jr.dahe.cn/html/[a-z]*/[a-z]*/[0-9]{6}/[0-9]{2}-[0-9]*.html",
				"http://it.dahe.cn/[0-9]*/[0-9]*b.shtml",
				"http://auto.dahe.cn/[a-z]*/[a-z0-9]*/[0-9]{4}-[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://jdwt.dahe.cn/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://edu.dahe.cn/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://art.dahe.cn/.*/.*.htm",
				"http://zmd.dahe.cn/category/.*.html",
				"http://kf.dahe.cn/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://xnc.dahe.cn/article/show/[0-9]*.html",
				"http://xc.dahe.cn/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://py.dahe.cn/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://newsrank.dahe.cn/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://xy.dahe.cn/[0-9]{4}/[0-9]{2}-[0-9]{2}/[0-9]*.html",
				"http://sh.dahe.cn/.*.html",
				"http://yazz.dahe.cn/index.php[?]s=.*.html"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		String commnetNum=null;
		String clickNum=null;
		Document doc=Jsoup.parse(htmltxt);
		doc.select("#copyright").remove();
		if(url.startsWith("http://law.dahe.cn/")){
			title=doc.select("div#article_title").text();
			content =HtmlCleaner.getContentHtml(url, doc.select("div#article_content"));
			date=JSoupUtils.matchDate(doc, "div#article_info");
//			commnetNum=doc.select("div.content > div.clearfix > div.left-cont > div.title-s > span").text();
		}else if(url.startsWith("http://photo.dahe.cn/")){
			title=doc.select("div#contentMain h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#imgBCon1"));
			date=JSoupUtils.matchDate(doc, "span.sTime");
		}else if(url.startsWith("http://jr.dahe.cn/")){
			title=doc.select("div.title h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.ArticleCnt"));
			date=JSoupUtils.matchDate(doc, "div.title");
		}else if(url.startsWith("http://it.dahe.cn/")){
			title=doc.select("div.nr_cen h1").text();
			content= HtmlCleaner.getContentHtml(url,doc.select("div.nry_zw"));
			date=JSoupUtils.matchDate(doc, "span.zecc");
		}else if(url.startsWith("http://auto.dahe.cn/")){
			title=doc.select("div#missde h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#missde table.msg"));
			date=JSoupUtils.matchDate(doc, "span.sTime");
		}else if(url.startsWith("http://edu.dahe.cn/")){
			title=doc.select("div.newstitle").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#news-l5"));
			date=JSoupUtils.matchDate(doc, "div#news-l4");
		}else if(url.startsWith("http://art.dahe.cn/")){
			title=doc.select("div#nc h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#tLContent"));
		}else if(url.startsWith("http://zmd.dahe.cn/")){
			title=doc.select("div#newsTitle").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#detail"));
			date=JSoupUtils.matchDate(doc, "div#newsInfo");
		}else if(url.startsWith("http://xnc.dahe.cn/")){
			title=doc.select("div#news_title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.content_box"));
			date=JSoupUtils.matchDate(doc, ".date_i");
		}else if(url.startsWith("http://sh.dahe.cn/")){
			title=doc.select("h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#Cnt-Main-Article-QQ"));
			date=JSoupUtils.matchDate(doc, "span.pubTime");
		}else if(url.startsWith("http://yazz.dahe.cn/")){
			title=doc.select("h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div.info_con"));
			date=JSoupUtils.matchDate(doc, "div.info_details");
		}else{
			title=doc.select("#main h1").text();
			content =HtmlCleaner.getContentHtml(url, doc.select("div#mainCon"));
			date=JSoupUtils.matchDate(doc, "#pubtime_baidu");
		}

		if(date==null){
			date=DateUtils.matchDate(url);
		}
		if(title==null || title.trim().length()==0){
			title=doc.select("title").text();
			title=title.replace("-大河网", "");
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#mainCon"));
		}
		String author=JSoupUtils.matchAuthor(doc, "来源:");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		DaheNewsAnalyse a=new DaheNewsAnalyse();
//		String url="http://opinion.dahe.cn/2012/11-19/101760962.html";
//		String url="http://news.dahe.cn/2012/11-22/101770349.html";
//		String url="http://law.dahe.cn/2012/1122/57183.html";
//		String url="http://auto.dahe.cn/suipian/center4/2013-01-07/3472.html";
//		String url="http://it.dahe.cn/816756/552103784357b.shtml";
//		String url="http://zmd.dahe.cn/category/10101/2013/01/04/2013-01-04_231834_10101.shtml";
//		String url="http://xc.dahe.cn/2013/01-07/101892927.html";
//		String url="http://py.dahe.cn/2012/11-28/101787204.html";
//		String url="http://xy.dahe.cn/2013/01-07/101893073.html";
		String url="http://news.dahe.cn/2014/08-07/103302159.html";
//		String url="http://sh.dahe.cn/shenghuo/2013-01-07/46476.html";
//		String url="http://xnc.dahe.cn/article/show/27303.html";
//		String url="http://kf.dahe.cn/2013/01-07/101892474.html";
//		String url="http://art.dahe.cn/msdt/t20121220_1859009.htm";
//		String url="http://edu.dahe.cn/2013/01-07/101892698.html";
//		String url="http://jdwt.dahe.cn/2012/12-18/101842270.html";
//		String url="http://photo.dahe.cn/2013/01-05/101886583.html";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
